package com.sbtr.common.file.dto;


import com.sbtr.common.file.utils.AbstractNoModelDataListener;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Data
@Accessors(chain = true)
public class FileContentDto {
    @ApiModelProperty(value = "数据标题")
    List<String> title;

    @ApiModelProperty(value = "数据内容")
    List<LinkedHashMap<Integer, String>> content;

    public FileContentDto() {
    }

    public FileContentDto(AbstractNoModelDataListener abstractNoModelDataListener) {
        this.title = abstractNoModelDataListener.titleList;
        this.content = abstractNoModelDataListener.resultDataList;
    }

    public FileContentDto(List<String> title, List<LinkedHashMap<Integer, String>> content) {
        this.title = title;
        this.content = content;
    }

    /**
     * 转换成集合嵌套Map(key标题，value内容)
     *
     * @return
     */
    public List<LinkedHashMap<String, String>> getListMap() {
        List<LinkedHashMap<String, String>> list = new ArrayList<>();
        for (int i = 0; i < this.content.size(); i++) {
            LinkedHashMap<String, String> params = new LinkedHashMap<>();
            Map<Integer, String> content2 = this.content.get(i);
            for (Map.Entry<Integer, String> entry : content2.entrySet()) {
                params.put(title.get(entry.getKey()), content2.get(entry.getKey()));
            }
            list.add(params);
        }
        return list;
    }

    /**
     * 列标题内容转换结构转换成行结构(key下标，value内容)
     *
     * @return
     */
    public List<LinkedHashMap<Integer, String>> lineGetListMap() {
        List<LinkedHashMap<Integer, String>> list = new ArrayList<>();

        int listMaxLength = 0; //列最长的长度
        for (int i = 0; i < this.content.size(); i++) {//行迭代
            Map<Integer, String> content2 = this.content.get(i);
            if (listMaxLength < content2.size()) listMaxLength = content2.size();
        }

        for (int j = 0; j < listMaxLength - 1; j++) {//列迭代
            for (int i = 0; i < this.content.size(); i++) {//行迭代
                Map<Integer, String> content2 = this.content.get(j);
                if (CollectionUtils.isEmpty(list) || list.size() < i + 1) {
                    LinkedHashMap<Integer, String> param = new LinkedHashMap<>();
                    param.put(j, content2.get(i) == null ? "" : content2.get(i));
                    list.add(param);
                } else {
                    list.get(i).put(j, content2.get(i));
                }
            }
        }
        return list;
    }

    /**
     * 转换成集合嵌套数组字符串
     *
     * @return
     */
    public List<String[]> getListStr() {
        List<String[]> list = new ArrayList<>();

        for (int i = 0; i < this.content.size(); i++) {
            String[] str = new String[this.content.get(i).size()];
            for (int j = 0; j < this.content.get(i).size(); j++) {
                str[j] = this.content.get(i).get(j);
            }
            list.add(str);
        }
        return list;
    }

    /**
     * 转换成集合嵌套集合字符串
     *
     * @return
     */
    public List<List<String>> getListList() {
        List<List<String>> list = new ArrayList<>();

        for (int i = 0; i < this.content.size(); i++) {
            List<String> str = new ArrayList<>();
            this.content.get(i).forEach((k, v) -> str.add(v));
            list.add(str);
        }

        return list;
    }

    /**
     * 获取map的title
     *
     * @return
     */
    public LinkedHashMap<Long, String> getMapTitle() {
        LinkedHashMap<Long, String> longStringLinkedHashMap = new LinkedHashMap<>();
        for (int i = 0; i < title.size(); i++) {
            longStringLinkedHashMap.put(Long.valueOf(i), title.get(i));
        }
        return longStringLinkedHashMap;
    }

    public void setTitle(List<String> title) {
        Set<String> title2 = new HashSet<>(title);
        if (title.size() != title2.size()) {
            throw new IllegalArgumentException("不能存在相同标题！！");
        }
        this.title = title;
    }

    /**
     * 转换成集合嵌套数组字符串(列)
     *
     * @return
     */
    public List<String[]> getVerticalList() {
        List<String[]> list = new ArrayList<>();

        int listMaxLength = 0; //列最长的长度
        for (int i = 0; i < this.content.size(); i++) {//行迭代
            Map<Integer, String> content2 = this.content.get(i);
            if (listMaxLength < content2.size()) listMaxLength = content2.size();
        }
        for (int j = 0; j < listMaxLength; j++) {
            String[] str = new String[this.content.size()];
            list.add(str);
        }

        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < this.content.size(); j++) {
                list.get(i)[j]= String.valueOf(this.content.get(j).get(i));
            }
        }
        return list;
    }


}


